﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="editmarkup.aspx.cs" Inherits="NegevGas.Extensions.markup.editmarkup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
    <title>Edit Markup</title>
    <link rel="stylesheet" href="../../styles/gt.css" type="text/css">

    <script language="javascript" src="../../viewerfiles/browserdetect.js"></script>

    <script language="javascript">
   
		var CMD_ADD_POINT		= 1;
		var CMD_ADD_LINE 		= 2;
        var CMD_ADD_LINESTRING 	= 3;
		var CMD_ADD_RECTANGLE 	= 4;
		var CMD_ADD_POLYGON 	= 5;
		var CMD_ADD_BUFFER      = 8;
		var CMD_DELETE 			= 6;
		var CMD_UPDATE 			=7;
	
		var EDIT_DEFAULT_HELP = "עליך לבחור את סוג הרכיב לדגימה.";
		var EDIT_POINT_HELP = "להצבת הנקודה, הקש על המיקום במפה.";
		var EDIT_LINE_HELP = "Click and drag on the map to specify the start and end points of the line.";
		var EDIT_LINESTRING_HELP = "הקש באמצעות העכבר להגדרת נתיב הקו, השתמש ב- Ctrl-Click לסיום קליטה.";
		var EDIT_RECTANGLE_HELP = "הקש באמצעות העכבר במפה להצבת פינה וגרור את העכבר להגדרת ריבוע תוחם.";
		var EDIT_POLYGON_HELP = "הקש באמצעות העכבר להגדרת נקודות קיצון המצולע, השתמש ב- Ctrl-Click לסיום קליטה.";
		var EDIT_BUFFER_HELP = "בחר הרחיב (או מספר רחיבים) במפה ליצירת תחום חייץ. לסיום לחצו שוב על כפתור \"תחום חייץ\"";

	    
	    var originalOnSelectionChanged = parent.parent.OnSelectionChanged;
	    parent.parent.OnSelectionChanged = function (){
	        originalOnSelectionChanged();
	        if(parent.parent.mapFrame.selection.count > 0)
	        {
	            document.getElementById("bufferBtn").disabled = false;
	        }
	        else
	        {
	            document.getElementById("bufferBtn").disabled = true;
	        }
	    }
	    
		function SetDigitizeInfo(text)
		{
			var digitizeInfo = document.getElementById("digitizeInfo");
			digitizeInfo.innerHTML = text;
		}

		function SubmitCommand(cmd)
		{
			var commandInput = document.getElementById("commandInput");
			commandInput.value = cmd;
			
			var editForm = document.getElementById("editForm");
			editForm.submit();
		}
	
		function AddPoint()
		{
			SetDigitizeInfo(EDIT_POINT_HELP);
			parent.parent.mapFrame.DigitizePoint(OnPointDigitized);
		}

		function AddLine()
		{
			SetDigitizeInfo(EDIT_LINE_HELP);
			parent.parent.mapFrame.DigitizeLine(OnLineStringDigitized);
		}

		function AddLineString()
		{
			SetDigitizeInfo(EDIT_LINESTRING_HELP);
			parent.parent.mapFrame.DigitizeLineString(OnLineStringDigitized);
		}

		function AddRectangle()
		{
			SetDigitizeInfo(EDIT_RECTANGLE_HELP);
			parent.parent.mapFrame.DigitizeRectangle(OnRectangleDigitized);
		}

		function AddPolygon()
		{
			SetDigitizeInfo(EDIT_POLYGON_HELP);
			parent.parent.mapFrame.DigitizePolygon(OnPolyonDigitized);
		}

		function AddBuffer()
		{
			if(parent.parent.mapFrame.selection.count > 0)
			{
			    var success = PromptAndSetMarkupText();
			    if(success)
			    {
			        SubmitCommand(CMD_ADD_BUFFER);
			    }
			}
			else
			{
			    SetDigitizeInfo(EDIT_BUFFER_HELP);
			}
		}

		function PromptAndSetMarkupText()
		{
			var textInput = document.getElementById("textInput");

			textLabel = window.prompt("Enter a label for the markup item", "");
			textInput.value = (textLabel != null) ? textLabel : "";

			return textLabel != null;
		}
	
		function OnPointDigitized(point)
		{
			PromptAndSetMarkupText();			

			var geometryInput = document.getElementById("geometryInput");
			geometryInput.value = point.X + "," + point.Y;
			
			SubmitCommand(CMD_ADD_POINT);
		}

		function OnLineStringDigitized(lineString)
		{
			PromptAndSetMarkupText();			

			var geomText = lineString.Count;
			for (var i = 0; i < lineString.Count; i++)
			{
				geomText += "," + lineString.Point(i).X + "," + lineString.Point(i).Y;
			}
		
			var geometryInput = document.getElementById("geometryInput");
			geometryInput.value = geomText;

			SubmitCommand(CMD_ADD_LINESTRING);
		}

		function OnRectangleDigitized(rectangle)
		{
			PromptAndSetMarkupText();			

			var geometryInput = document.getElementById("geometryInput");
			geometryInput.value = "5," 
				+ rectangle.Point1.X + "," + rectangle.Point1.Y + ","
				+ rectangle.Point2.X + "," + rectangle.Point1.Y + ","
				+ rectangle.Point2.X + "," + rectangle.Point2.Y + ","
				+ rectangle.Point1.X + "," + rectangle.Point2.Y + ","
				+ rectangle.Point1.X + "," + rectangle.Point1.Y;

			SubmitCommand(CMD_ADD_RECTANGLE);
		}
	
		function OnPolyonDigitized(polygon)
		{
			PromptAndSetMarkupText();			

			var geomText = polygon.Count;
			for (var i = 0; i < polygon.Count; i++)
			{
				geomText += "," + polygon.Point(i).X + "," + polygon.Point(i).Y;
			}
		
			var geometryInput = document.getElementById("geometryInput");
			geometryInput.value = geomText;
			SubmitCommand(CMD_ADD_POLYGON);
		}
	
		function SelectMarkup()
		{          
   
			markupFeatures = document.getElementById("markupFeatures");
			
            reqParams = "MAPNAME=" + encodeURIComponent('<%= GetParameter(args, "MAPNAME") %>');
			reqParams += "&SESSION=" + encodeURIComponent('<%= GetParameter(args, "SESSION") %>');
			reqParams += "&OPENMARKUP=" + encodeURIComponent('<%= GetParameter(args, "OPENMARKUP") %>');
			reqParams += "&MARKUPFEATURE=" + markupFeatures.value;
            
            if(msie)
                reqHandler = new ActiveXObject("Microsoft.XMLHTTP");
            else
                reqHandler = new XMLHttpRequest();
                
            reqHandler.open("POST", "getselectionxml.aspx", false);
            reqHandler.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

			reqHandler.send(reqParams);
			if(reqHandler.responseXML)
			{
            
			//	parent.parent.mapFrame.SetSelectionXML(reqHandler.responseText);
                parent.parent.SetSelectionXML(reqHandler.responseText);
			}
		}
	
		function ZoomSelection()
		{
       
		    parent.parent.mapFrame.ZoomSelection();            
        }

		function ClearSelection()
		{
		    parent.parent.mapFrame.ClearSelection();            
        }
        
		function DeleteMarkup()
		{
			if(confirm("הרכיב ימחק. להמשיך?"))

			{
			    SubmitCommand(CMD_DELETE);
			}
		}
		
		function UpdateMarkup()
		{
			SubmitCommand(CMD_UPDATE);
		}
	
		function CloseEditor()
		{
			var editForm = document.getElementById("editForm");
			editForm.action = '<%= String.IsNullOrEmpty(Request["RETURNURL"]) ? "markupmain.aspx" : Request["RETURNURL"] %>';
			
			editForm.submit();
		}
	
		function OnMarkupFeatureChange()
		{
            var markupFeatures = document.getElementById("markupFeatures");
			var updateTextInput = document.getElementById("updateTextInput");
            var selectBtn = document.getElementById("selectBtn");
            var deleteBtn = document.getElementById("deleteBtn");
			var updateBtn = document.getElementById("updateBtn");
			var clearBtn = document.getElementById("cleartBtn");
			var zoomBtn = document.getElementById("zoomBtn");
			
			if (markupFeatures.selectedIndex >= 0)
			{
				value = markupFeatures.options[markupFeatures.selectedIndex].text;
				if (value != "[no text]")
					updateTextInput.value = value;
				else
					updateTextInput.value = '';
					
				selectBtn.disabled = false;
				deleteBtn.disabled = false;
				updateBtn.disabled = false;
				clearBtn.disabled = false;
				zoomBtn.disabled = false;
			}
			else
			{
				updateTextInput.value = '';
				selectBtn.disabled = true;
				deleteBtn.disabled = true;
				updateBtn.disabled = true;
				clearBtn.disabled = true;
				zoomBtn.disabled = true;
			}
		} 
	
		function OnLoad()
		{
			OnMarkupFeatureChange();
	        if(parent.parent.mapFrame.selection.count > 0)
	        {
	            document.getElementById("bufferBtn").disabled = false;
	        }

		<% if (refreshMap) { %>
			parent.parent.Refresh();
		<% } %>
			SetDigitizeInfo(EDIT_DEFAULT_HELP);
		}
		function OnUnload()
		{
		    parent.parent.OnSelectionChanged = originalOnSelectionChanged;
		}
    </script>

</head>
<body dir="rtl" onload="OnLoad()" onunload="OnUnload()" style="margin: 5px">
    <% if (errorMsg == null) { %>
    <form action="editmarkup.aspx" method="post" enctype="application/x-www-form-urlencoded"
    id="editForm" target="_self">
    <table class="RegText" border="0" cellspacing="0" width="100%">
        <tr>
            <td colspan="2" class="Title">
                עדכון שכבת -
                <%= markupEditor.GetMarkupName() %><hr />
            </td>
        </tr>
        <tr>
            <td colspan="2" class="SubTitle">
               הוספת רכיבי השכבה

            </td>
        </tr>
        <tr>
            <td colspan="2">
                 דגימת רכיבים במפה:
            </td>
        </tr>
        <tr>
            <td colspan="2" height="2px">
            </td>
        </tr>
      
        <tr>
            <td>
                <input class="Ctrl" id="Button1" type="button" onclick="AddPoint()" value="נקודה"
                    style="width: 45px">
                <!--input class="Ctrl" id="lineBtn" type="button" onClick="AddLine()" value="Line" style="width:60px"-->
                <input class="Ctrl" id="Button2" type="button" onclick="AddLineString()" value="קו"
                    style="width: 45px">
            </td>
            <td>
                <input class="Ctrl" id="Button3" type="button" onclick="AddRectangle()" value="ריבוע"
                    style="width: 45px">
                <input class="Ctrl" id="Button4" type="button" onclick="AddPolygon()" value="מצולע"
                    style="width: 45px">
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input class="Ctrl" id="bufferBtn" type="button" onclick="AddBuffer()" value="תחום חייץ"

                    disabled="disabled" style="width: 60px" />
            </td>
        </tr>
        <tr>
            <td colspan="2" height="2px">
            </td>
        </tr>
        <tr>
            <td class="InfoText" colspan="2" id="digitizeInfo">
            </td>
        </tr>
        <tr>
            <td colspan="2">
            </td>
        </tr>
        <tr>
            <td colspan="2" class="SubTitle">
                                עדכון רכיבים

            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input class="Ctrl" name="UPDATETEXT" type="text" id="updateTextInput" maxlength="55"
                    style="width: 125px">
                <input class="Ctrl" id="updateBtn" type="button" onclick="UpdateMarkup()" value="עדכון טקסט"

                    style="width: 70px"><br>
            </td>
        </tr>
        <tr>
            <td class="RegText" colspan="2">
                <select name="MARKUPFEATURE" size="15" class="Ctrl" id="markupFeatures" onchange="OnMarkupFeatureChange()"
                    style="width: 100%">
                    <%
				    String selected = "selected";
				    foreach(var item in markupFeatures)
				    {
                    %>
                    <option value="<%= item.Key %>" <%=selected %>>
                        <%= (item.Value.Length > 0) ? item.Value : "[no text]" %></option>
                    <%
					    selected = ""; 
				    }
                    %>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" height="2px">
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Literal ID="UnSuccessText" runat="server" Visible="false"></asp:Literal>
                <input class="Ctrl" id="selectBtn" type="button" onclick="SelectMarkup()" value="בחר רכיב"
                    style="width: 90px">
                <input class="Ctrl" id="deleteBtn" type="button" onclick="DeleteMarkup()" value="מחק רכיב"
                    style="width: 90px"><br />
                <input class="Ctrl" id="zoomBtn" type="button" onclick="ZoomSelection()" value="התמקדות לבחירה"
                    style="width: 90px">
                <input class="Ctrl" id="cleartBtn" type="button" onclick="ClearSelection()" value="נקה בחירה"
                    style="width: 90px">
            </td>
        </tr>
        <tr>
            <td colspan="2" height="2px">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <hr>
                <input class="Ctrl" name="" type="button" onclick="CloseEditor()" value="חזור" style="width: 60px">
				<span style="margin: 0 10px">
				<%= String.Format("<a href=\"../../Extensions/ProjectManager/GetSdfFile.aspx?session={0}&map={1}&layoutId={2}&layerDefinition={3}\">הורדה כקובץ</a>", GetParameter(args, "SESSION"), GetParameter(args, "MAPNAME"), GetParameter(args, "LAYOUT"), GetParameter(args, "OPENMARKUP")) %>
				</span>
			</td>
        </tr>
    </table>
    <input name="SESSION" type="hidden" value="<%= GetParameter(args, "SESSION") %>">
    <input name="MAPNAME" type="hidden" value="<%= GetParameter(args, "MAPNAME") %>">
    <input name="LAYOUT" type="hidden" value="<%= GetParameter(args, "LAYOUT") %>">
    <input name="OPENMARKUP" type="hidden" value="<%= GetParameter(args, "OPENMARKUP") %>">
    <input name="EDITCOMMAND" type="hidden" value="" id="commandInput">
    <input name="GEOMETRY" type="hidden" value="" id="geometryInput">
    <input name="TEXT" type="hidden" value="" id="textInput">
    </form>
    <% } else { %>
    <table class="RegText" border="0" cellspacing="0" width="100%%">
        <tr>
            <td class="Title">
                Error<hr>
            </td>
        </tr>
        <tr>
            <td>
                <%= errorMsg %>
            </td>
        </tr>
        <tr>
            <td>
                <%= errorDetail %>
            </td>
        </tr>
    </table>
    <% } %>
</body>
</html>
